/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkUtil/MkIni.h"
#include "MkUtil/MkFile.h"
#include <gtest/gtest.h>

TEST(MkIni, iniTest)
{
    MkString FileName = "config.ini";
    CMkIni Ini;
    Ini.SetProfileString("app.key", "value");
    Ini.SetProfileString("app.key2", "value2");
    Ini.SetProfileDouble("app2.key", 0.5);
    Ini.SetProfileDouble("app2.key2", 0.52);
    Ini.SetProfileUint32("app3.key", 1);
    Ini.SetProfileUint32("app3.key2", 2);
    Ini.SetProfileBoolean("app4.key", TRUE);
    Ini.SetProfileBoolean("app4.key2", FALSE);
    MkVectorString VecAppNames;
    Ini.GetProfileAppNames(VecAppNames);
    EXPECT_EQ(4, VecAppNames.size());
    MkVectorString VecKeys;
    Ini.GetProfileKeyNames("app", VecKeys);
    EXPECT_EQ(2, VecKeys.size());
    Ini.WriteFile(FileName);

    CMkIni Ini2;
    Ini2.LoadFile(FileName);
    MkString strValue = Ini2.GetProfileString("app.key2");
    EXPECT_STREQ(strValue.c_str(), "value2");
    Ini2.SetProfileString("app.key2", "value");
    strValue = Ini2.GetProfileString("app.key2");
    EXPECT_STREQ(strValue.c_str(), "value");

    double dValue = Ini2.GetProfileDouble("app2.key");
    Uint32 uValue = Ini2.GetProfileUint32("app3.key");
    BOOL bValue = Ini2.GetProfileBoolean("app4.key");
    EXPECT_EQ(dValue, 0.5);
    EXPECT_EQ(uValue, 1);
    EXPECT_EQ(bValue, TRUE);
    dValue = Ini2.GetProfileDouble("app2.key2", 0.1);
    uValue = Ini2.GetProfileUint32("app3.key2", 10);
    bValue = Ini2.GetProfileBoolean("app4.key2", FALSE);
    EXPECT_EQ(dValue, 0.52);
    BOOL TMp = 0.52 == dValue;
    EXPECT_EQ(uValue, 2);
    EXPECT_EQ(bValue, FALSE);

    dValue = Ini2.GetProfileDouble("app2.key3", 0.1);
    uValue = Ini2.GetProfileUint32("app3.key3", 10);
    bValue = Ini2.GetProfileBoolean("app4.key3", TRUE);
    EXPECT_EQ(dValue, 0.1);
    EXPECT_EQ(uValue, 10);
    EXPECT_EQ(bValue, TRUE);

    dValue = Ini2.GetProfileDouble("aa", 0.2);
    uValue = Ini2.GetProfileUint32("aa", 111);
    bValue = Ini2.GetProfileBoolean("aaddf", TRUE);
    EXPECT_EQ(dValue, 0.2);
    EXPECT_EQ(uValue, 111);
    EXPECT_EQ(bValue, TRUE);
    strValue = Ini2.GetProfileString("aa", "bb", "cc");
    EXPECT_STREQ("cc", strValue.c_str());

    Uint32 ErrorCode = Ini2.SetProfileBoolean("aa", FALSE);
    EXPECT_NE(ErrorCode, NoneError);
    ErrorCode = Ini2.SetProfileDouble("aa", 0.0);
    EXPECT_NE(ErrorCode, NoneError);
    ErrorCode = Ini2.SetProfileUint32("aa", 1);
    EXPECT_NE(ErrorCode, NoneError);
    ErrorCode = Ini2.SetProfileString("aa", "aa");
    EXPECT_NE(ErrorCode, NoneError);
}

TEST(MkIni, SourceIni)
{
    MkString Path = MkString(MkUtSourcePath) + "/test.ini";
    CMkIni Ini;
    Uint32 ErrorCode = Ini.LoadFile(Path);
    EXPECT_EQ(ErrorCode, NoneError);
}